import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import javax.swing.*;

public class GerarRelatorio {
    private static RelatorioControl control = new RelatorioControl();
    private static JPanel pNome, pSaida;
    private static JTextField placa, nome, nomeSaida;
    private static JButton button;
    private static JComboBox op;
    private static JPanel pPlaca;
    private static JComboBox mes;
    private JPanel pMes;
    private static JComboBox ano;
    
    public GerarRelatorio() {
    }
    
    public JPanel novoForm(){
        JPanel form = new JPanel();
        form.setLayout(new FlowLayout());
       
        JPanel panel = new JPanel(new GridLayout(6,1));
        form.add(panel);
      
        String[] array = new String[2];
        array[0] = "Todos os caminhões";
        array[1] = "Um único caminhão";
        JComboBox choice = new JComboBox(array);
        op = choice;
        panel.add(choice);
        GerarRelatorio.ChoiceListener itemListener = new GerarRelatorio.ChoiceListener() {};
        choice.addItemListener(itemListener);
      
        JPanel row = new JPanel();
        pPlaca = row;
        JLabel label = new JLabel("Placa Caminhão: "); // construct Label
        row.add(label);
        JTextField text = new JTextField("XXX0000", 10);
        placa = text;
        row.add(text);
        panel.add(row);
        pPlaca.setVisible(false);
        
        row = new JPanel();
        pMes = row;
        String[] array2 = {"1","2","3","4","5","6","7","8","9","10","11","12"};
        Calendar cal = Calendar.getInstance();
        String[] array3 = new String[50];
        for(int i=0;i<50;i++)
            array3[i] = (cal.get(Calendar.YEAR)-i)+"";
        choice = new JComboBox(array2);
        mes = choice;
        mes.setSelectedIndex(cal.get(Calendar.MONTH));
        label = new JLabel("Mês: "); // construct Label
        row.add(label);
        row.add(choice);
        choice = new JComboBox(array3);
        ano = choice;
        label = new JLabel("Ano: "); // construct Label
        row.add(label);
        row.add(choice);
        panel.add(row);

        button = new JButton("Gerar Relatório"); // construct Button
        panel.add(button);
        GerarRelatorio.ButtonListener buttonListener = new GerarRelatorio.ButtonListener();
        button.addActionListener(buttonListener);
        
        form.setSize(300, 300);
        form.setVisible(true);
        return form;
    }
    
    private static class ChoiceListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            switch (op.getSelectedIndex()) {
                case 0:
                    pPlaca.setVisible(false);
                    break;
                case 1:
                    pPlaca.setVisible(true);
                    break;
            }
        }
    }
    
    private static class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String codigo = placa.getText();
            int m = mes.getSelectedIndex()+1;
            Calendar cal = Calendar.getInstance();
            int a = cal.get(Calendar.YEAR)-ano.getSelectedIndex();
            
            switch (op.getSelectedIndex()) {
                case 0:
                    control.relatorioTodos(m, a);
                    break;
                case 1:
                    control.relatorioUnico(codigo, m, a);
                    break;
            }
        }
    }
   
}